package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.SysMenu;
import com.hongpu.dto.SysMenuDto;
import com.hongpu.mapper.SysMenuMapper;
import com.hongpu.service.SysMenuService;
import com.hongpu.utils.AllMenuDataTreeUtils;
import com.hongpu.utils.MenuTreeUtils;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.MenuTreeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Michael zhang
 * @description 针对表【sys_permission(菜单权限表)】的数据库操作Service实现
 * @createDate 2025-06-05 09:11:31
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService {
    @Autowired
    SysMenuMapper baseMapper;

    /**
     * 新增
     *
     * @param sysMenu
     * @return
     */
    @Override
    public ResultDto add(SysMenu sysMenu) {
        try {
            boolean add = baseMapper.add(sysMenu);
            return add ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.fail(e.getMessage());
        }



    }

    /**
     * 修改
     *
     * @param sysMenu
     */
    @Override
    public boolean update(SysMenu sysMenu) {
        sysMenu.setUpdateBy(SecurityContextUtil.getUserId());
        return baseMapper.update(sysMenu);
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @Override
    public boolean delete(Long menuId) {
        return baseMapper.delete(menuId,SecurityContextUtil.getUserId());
    }

    /**
     * 启用菜单
     *
     * @param menuId
     * @return
     */
    @Override
    public boolean active(Long menuId) {
        return baseMapper.active(menuId,SecurityContextUtil.getUserId());
    }

    /**
     * 角色管理用中到的菜单下拉列表树
     */
    @Override
    public ResultDto menuTreeList() {
        try  {
            List<SysMenu> list = baseMapper.menuList();
            MenuTreeUtils menuTreeUtils = new MenuTreeUtils(list);
            List<MenuTreeVo> tree = menuTreeUtils.buildTree();
            return ResultDto.ok(tree);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 数据列表
     *
     * @param sysMenuDto
     */
    @Override
    public ResultDto list(SysMenuDto sysMenuDto) {
        try {
            List<SysMenu> menuList = baseMapper.getList(sysMenuDto);
            AllMenuDataTreeUtils menuTreeUtils = new AllMenuDataTreeUtils(menuList);
            List<SysMenu> menuTreeVos = menuTreeUtils.buildTree();
            return ResultDto.ok(menuTreeVos);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }

    }
}




